package gw.com.android.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.util.Util;
import com.gwtsz.gts2.hx.R;

import java.io.File;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.model.ConfigType;
import gw.com.android.presenter.AdverPagePresenter;
import gw.com.android.utils.PackStatis;
import www.com.library.model.DataItemDetail;

/**
 * 这个，是启动app的时候 跳转3，2，1的那个页面
 * @author jett
 * @since 2017-09-05.
 */
public class AdvertiseActivity extends BaseActivity {

    @BindView(R.id.iv_ad)
    ImageView adImage;
    @BindView(R.id.tv_skip)
    TextView skip;

    private Runnable r;

    @Override
    protected void setAcitivityParam() {
        super.setAcitivityParam();
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.activity_advertise;
    }

    @Override
    protected void initLayoutView() {
    }

    @Override
    protected void initViewData() {
        File imageFile = AdverPagePresenter.getInstance().getImageFile();
        if (imageFile.exists()) {
            Glide.with(this).load(imageFile).into(adImage);
            AdverPagePresenter.getInstance().onAdvertiseShowed();
            setListener();
            long duration = AdverPagePresenter.getInstance().getDuration();
            schedule((int) (duration / 1000));
        } else {
            toMain();
        }
    }

    private void schedule(final int second) {
        skip.setText(getSkipText(second));
        if (second <= 0) {
            toMain();
            return;
        }
        r = new Runnable() {
            @Override
            public void run() {
                schedule(second - 1);
            }
        };
        AppMain.getApp().getHandler().postDelayed(r, 1000);
    }

    private CharSequence getSkipText(int second) {
        String text = AppMain.getApp().getString(R.string.advertise_skip);
        return String.format(Locale.getDefault(), text, second);
    }

    private void setListener() {
        final String title = AdverPagePresenter.getInstance().title;
        final String linkUrl = AdverPagePresenter.getInstance().linkUrl;
        if (TextUtils.isEmpty(linkUrl)) {
            return;
        }
        adImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (r != null) {
                    AppMain.getApp().getHandler().removeCallbacks(r);
                    r = null;
                }
                DataItemDetail item = new DataItemDetail();
                item.setStringValue(ConfigType.CONFIG_TYPE_KEY_TAG, ConfigType.ADS_DETAIL_TAG);
                item.setStringValue(ConfigType.CONFIG_TYPE_TYPE_URL_TAG, linkUrl);
                item.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, title);

                Intent intent = new Intent(AdvertiseActivity.this, WebPageActivity.class);
                intent.putExtra("left_title", AppMain.getAppString(R.string.btn_back));
                intent.putExtra("mItem", item);
                intent.putExtra("awake", getIntent().getBooleanExtra("awake", false));
                intent.putExtra("fromAd", true);
                Bundle extras = getIntent().getExtras();
                if (extras != null) {
                    intent.putExtras(extras);
                }
                startActivity(intent);
                finish();
            }
        });
    }

    @OnClick(R.id.tv_skip)
    void skip(View view) {
        if (r != null) {
            AppMain.getApp().getHandler().removeCallbacks(r);
        }
        toMain();
    }

    private void toMain() {
        r = null;
        if (getIntent().getBooleanExtra("awake", false)) {
            // 从后台唤醒后显示的广告
            finish();
            return;
        }
        if (getIntent().getExtras() != null && getIntent().getIntExtra("where", 0) == AppContances.TYPE_FORM_NOTIFY) {
            // 来自于推送
            PackStatis.getToService(PackStatis.EventAction.START.getValue(), PackStatis.EventCategory.MAIN.getValue(), null, "2");
            ActivityManager.showMainTab(this, ConfigType.TAB_HOME_TAG, getIntent().getExtras());
        } else {
            PackStatis.getToService(PackStatis.EventAction.START.getValue(), PackStatis.EventCategory.MAIN.getValue(), null, "1");
            ActivityManager.showMainTab(this, ConfigType.TAB_HOME_TAG, 0);
        }
        finish();
    }

    @Override
    public void onBackPressed() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (r != null) {
            AppMain.getApp().getHandler().removeCallbacks(r);
            r = null;
        }
        if (Util.isOnMainThread()&&!this.isFinishing()){
            Glide.with(this).pauseRequests();
        }
    }

}
